using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class RestPlatform : MonoBehaviour
{
    public bool _isPlayerOn;
    private BoxCollider2D _BC;
    private int _settingLayer;

    void Start()
    {
        _BC = gameObject.GetComponent<BoxCollider2D>();
        InputManager.Instance.Reg_InputEvent(GameplayKeyCode.DownArrow, InputActionPhase.Started, Fall);
        _isPlayerOn = false;
        _BC.isTrigger = true;
        _settingLayer = gameObject.layer;
        gameObject.layer = 0;
    }

    void Update()
    {

    }

    private void Fall()
    {
        if (_isPlayerOn) _BC.isTrigger = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.transform.tag == "Player")
        {
            if ((collision.transform.position - gameObject.transform.position).y > 0.5f)
            {
                _BC.isTrigger = false;
                _isPlayerOn = true;
                gameObject.layer = _settingLayer;
            }
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.tag == "Player")
        {
            if (_isPlayerOn)
            {
                _BC.isTrigger = false;
                _isPlayerOn = true;
                gameObject.layer = _settingLayer;
            }
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.transform.tag == "Player")
        {
            _BC.isTrigger = true;
            _isPlayerOn = false;
            gameObject.layer = 0;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.tag == "Player")
        {
            _BC.isTrigger = true;
            _isPlayerOn = false;
            gameObject.layer = 0;
        }
    }
}
